import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedList;

public class num496 {
    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        Deque<Integer> stack = new LinkedList<>();
        HashMap<Integer,Integer> map = new HashMap<>();
        for (int i = 0; i < nums2.length; i++) {
            while (!stack.isEmpty() && stack.peek() < nums2[i]) {
                int num = stack.pop();
                map.put(num,nums2[i]);
            }
            stack.push(nums2[i]);
        }
        while (!stack.isEmpty()) {
            map.put(stack.pop(), - 1);
        }
        int[] nums = new int[nums1.length];
        for (int i = 0; i < nums1.length; i++) {
            nums[i] = map.get(nums1[i]);
        }
        return nums;
    }
}
